import win32api
import win32con

from ctypes import *
import time


class CDDdriver:
    def __init__(self):
        print("Load DD!")

        self.dd_dll = windll.LoadLibrary(r'D:\code\dd_key\master\dd.2024.07\dd.43390\dd43390.dll')
        time.sleep(2)

        st = self.dd_dll.DD_btn(0)  # DD Initialize
        if st == 1:
            print("OK")
        else:
            print("Error")
            exit(101)

    def press(self, key, delay=1):
        print(f"Press {key}")
        key_map = {1: 201, '1': 201,
                   2: 202, '2': 202,
                   3: 203, '3': 203}
        if key_map.get(key) is not None:
            self.dd_dll.DD_key(key_map[key], 1)  # 按下
            self.dd_dll.DD_key(key_map[key], 2)  # 放开
            time.sleep(delay * 0.001)

    def mouse_move(self, point):
        self.dd_dll.DD_mov(point[0], point[1])

    def left_click(self, delay_ms=100):
        # #1==L.down, 2==L.up, 4==R.down, 8==R.up, 16==M.down, 32==M.up
        # 左键按下
        self.dd_dll.DD_btn(1)
        time.sleep(0.001 * delay_ms)
        # 左键松开
        self.dd_dll.DD_btn(2)

    def right_click(self, delay_ms=100):
        # 左键按下
        self.dd_dll.DD_btn(4)
        time.sleep(0.001 * delay_ms)
        # 左键松开
        self.dd_dll.DD_btn(8)


class SimpleNumberKeysSimulator:
    def __init__(self):
        # 定义数字键1、2、3的虚拟键码
        self.driver = CDDdriver()
        self.press_and_release = self.driver.press
        self.press_by_str = self.driver.press


# 使用示例
if __name__ == "__main__":
    simulator = SimpleNumberKeysSimulator()
    simulator.press_by_str("123")  # 模拟按下1、2、3
    simulator.press_by_str("21")  # 模拟按下2、1
